﻿package cn.itamt.dofmenu {
	import cn.itamt.tmp.MenuBtn;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent; 

	/** 
          * 2009/10/29 ver0.1 
          * @author tamt 
          */ 
         public class DofMenu extends Sprite { 
                 private var base : Sprite; 
  
                 private var maxScale : Number = 2; 
                 private var minScale : Number = .5; 
  
                 private var btnNum : uint = 10; 
  
                 private var oBaseW : Number; 
  
                 public function DofMenu() { 
                         base = new Sprite(); 
                         base.x = base.y = 50; 
                          
                         var btn : MenuBtn; 
                         for(var i : int = 0;i < btnNum; i++) { 
                                 btn = new MenuBtn(i.toString()); 
                                 base.addChild(btn); 
                                 btn.x = i * MenuBtn.W; 
                         } 
                         oBaseW = base.width; 
                          
                         base.graphics.beginFill(0xffffff, 0); 
                         base.graphics.drawRect(0, 0, oBaseW, MenuBtn.H); 
                         base.graphics.endFill(); 
                          
                         addChild(base); 
                          
                         base.addEventListener(MouseEvent.ROLL_OVER, onRollOver); 
                         base.addEventListener(MouseEvent.ROLL_OUT, onRollOut); 
                         addEventListener(Event.ENTER_FRAME, onEnterFrame); 
                 } 
  
                 private function onRollOver(evt : MouseEvent) : void { 
                         base.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
                 } 
  
                 private function onRollOut(evt : MouseEvent) : void { 
                         resetBtns(); 
                         base.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
                 } 
  
                 private function onMouseMove(evt : MouseEvent) : void { 
                         var dist : Number; 
                         var btn : MenuBtn; 
                         var dx : Number; 
                         var dy : Number; 
                         for(var i : int = 0;i < btnNum; i++) { 
                                 btn = base.getChildAt(i) as MenuBtn; 
                                  
                                 dx = base.mouseX - (btn.x + MenuBtn.W / 2); 
                                 dy = base.mouseY - (btn.y + MenuBtn.H / 2); 
                                 dist = Math.sqrt(dx * dx + dy * dy); 
                                 if(dist > btnNum * MenuBtn.W)dist = btnNum * MenuBtn.W; 
                                  
                                 var scale : Number = this.maxScale + dist * (this.minScale - this.maxScale) / (btnNum * MenuBtn.W); 
                                  
                                 if(scale > this.maxScale)scale = this.maxScale; 
                                 if(scale < this.minScale)scale = this.minScale; 
                                  
                                 btn.tScale = scale; 
                         } 
                 } 
  
                 private function onEnterFrame(evt : Event) : void { 
                         var btn : MenuBtn; 
                         for(var i : int = 0;i < btnNum; i++) { 
                                 btn = base.getChildAt(i) as MenuBtn; 
                                 btn.scaleX += (btn.tScale - btn.scaleX) / 3; 
                                 btn.scaleY += (btn.tScale - btn.scaleY) / 3; 
                         } 
                          
                         var preBtn : MenuBtn; 
                         for(i = 0;i < btnNum; i++) { 
                                 btn = base.getChildAt(i) as MenuBtn; 
                                 if(i == 0) { 
                                         btn.x = 0; 
                                 } else { 
                                         preBtn = (base.getChildAt(i - 1) as MenuBtn); 
                                         btn.x = preBtn.x + preBtn.width; 
                                 } 
                         } 
                          
                         base.width = oBaseW; 
                         base.scaleY = base.scaleX; 
                 } 
  
                 private function resetBtns() : void { 
                         var btn : MenuBtn; 
                         for(var i : int = 0;i < btnNum; i++) { 
                                 btn = base.getChildAt(i) as MenuBtn; 
                                 btn.tScale = 1; 
                         } 
                 } 
         } 
 } 
 